package com.jet.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jet.core.model.vo.PageVo;
import com.jet.core.utils.IdUtil;
import com.jet.member.domain.dto.MemberTimeCardLogDto;
import com.jet.member.domain.query.MemberTimeCardLogQuery;
import com.jet.member.domain.entity.MemberTimeCardLog;
import com.jet.member.domain.vo.MemberTimeCardLogPageVo;
import com.jet.member.mapper.MemberTimeCardLogMapper;
import com.jet.member.service.IMemberTimeCardLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class MemberTimeCardLogServiceImpl extends ServiceImpl<MemberTimeCardLogMapper, MemberTimeCardLog> implements IMemberTimeCardLogService {

    @Override
    public void create(MemberTimeCardLogDto dto) {
        MemberTimeCardLog entity = new MemberTimeCardLog();
        entity.setId(IdUtil.generateLower());
        entity.setMemberId(dto.getMemberId());
        entity.setChangeType(dto.getChangeType());
        entity.setChangeTotal(dto.getTimeTotal());
        entity.setChangeAfter(dto.getChangeAfter());
        entity.setRemark(dto.getRemark());
        entity.setOrderNo(dto.getOrderNo());
        entity.setAccountId(dto.getAccountId());
        super.save(entity);
    }

    @Override
    public List<MemberTimeCardLog> findAllByMemberId(String memberId) {
        Wrapper<MemberTimeCardLog> wrapper = new LambdaQueryWrapper<MemberTimeCardLog>()
                .eq(MemberTimeCardLog::getMemberId, memberId);
        return super.list(wrapper);
    }

    @Override
    public PageVo<MemberTimeCardLogPageVo> page(MemberTimeCardLogQuery query) {
        if (query.getEndDate() != null) {
            query.setEndDate(query.getEndDate().plusDays(1));
        }
        Page<MemberTimeCardLog> tempQuery = new Page<>(query.getPageIndex(), query.getPageSize());
        Wrapper<MemberTimeCardLog> wrapper = new LambdaQueryWrapper<MemberTimeCardLog>()
                .eq(MemberTimeCardLog::getMemberId, query.getMemberId())
                .ge(query.getStartDate() != null, MemberTimeCardLog::getCreateTime, query.getStartDate())
                .lt(query.getEndDate() != null, MemberTimeCardLog::getCreateTime, query.getEndDate())
                .orderByDesc(MemberTimeCardLog::getCreateTime);
        IPage<MemberTimeCardLog> page = super.page(tempQuery, wrapper);
        List<MemberTimeCardLogPageVo> vos = page.getRecords().stream().map(x -> {
            MemberTimeCardLogPageVo vo = new MemberTimeCardLogPageVo();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return PageVo.result(page, vos);
    }
}
